package com.lifeverse.repository;

import com.lifeverse.entity.KnowledgeBase;
import com.lifeverse.entity.enums.KnowledgeStatus;
import com.lifeverse.entity.enums.KnowledgeType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 知识库数据访问接口
 */
@Repository
public interface KnowledgeBaseRepository extends JpaRepository<KnowledgeBase, Long> {

    /**
     * 根据生命体ID查找知识
     */
    List<KnowledgeBase> findByLifeEntityId(Long lifeEntityId);

    /**
     * 根据生命体ID和知识类型查找知识
     */
    List<KnowledgeBase> findByLifeEntityIdAndKnowledgeType(Long lifeEntityId, KnowledgeType knowledgeType);

    /**
     * 根据知识状态查找知识
     */
    List<KnowledgeBase> findByStatus(KnowledgeStatus status);

    /**
     * 查找活跃的知识
     */
    @Query("SELECT kb FROM KnowledgeBase kb WHERE kb.lifeEntityId = :lifeEntityId AND kb.status = 'VALIDATED' ORDER BY kb.importance DESC")
    List<KnowledgeBase> findActiveKnowledge(@Param("lifeEntityId") Long lifeEntityId);

    /**
     * 根据重要性查找知识
     */
    @Query("SELECT kb FROM KnowledgeBase kb WHERE kb.lifeEntityId = :lifeEntityId AND kb.importance >= :minImportance ORDER BY kb.importance DESC")
    List<KnowledgeBase> findImportantKnowledge(@Param("lifeEntityId") Long lifeEntityId, 
                                              @Param("minImportance") Double minImportance);

    /**
     * 查找高置信度的知识
     */
    @Query("SELECT kb FROM KnowledgeBase kb WHERE kb.lifeEntityId = :lifeEntityId AND kb.confidence >= :minConfidence ORDER BY kb.confidence DESC")
    List<KnowledgeBase> findHighConfidenceKnowledge(@Param("lifeEntityId") Long lifeEntityId, 
                                                   @Param("minConfidence") Double minConfidence);

    /**
     * 查找常用知识
     */
    @Query("SELECT kb FROM KnowledgeBase kb WHERE kb.lifeEntityId = :lifeEntityId AND kb.usageFrequency >= :minFrequency ORDER BY kb.usageFrequency DESC")
    List<KnowledgeBase> findFrequentlyUsedKnowledge(@Param("lifeEntityId") Long lifeEntityId, 
                                                    @Param("minFrequency") Integer minFrequency);

    /**
     * 查找最近使用的知识
     */
    @Query("SELECT kb FROM KnowledgeBase kb WHERE kb.lifeEntityId = :lifeEntityId AND kb.lastUsedAt >= :since ORDER BY kb.lastUsedAt DESC")
    List<KnowledgeBase> findRecentlyUsedKnowledge(@Param("lifeEntityId") Long lifeEntityId, 
                                                  @Param("since") LocalDateTime since);

    /**
     * 根据标签搜索知识
     */
    @Query("SELECT kb FROM KnowledgeBase kb WHERE kb.lifeEntityId = :lifeEntityId AND kb.tags LIKE %:tag%")
    List<KnowledgeBase> findByTag(@Param("lifeEntityId") Long lifeEntityId, @Param("tag") String tag);

    /**
     * 根据分类查找知识
     */
    List<KnowledgeBase> findByLifeEntityIdAndCategory(Long lifeEntityId, String category);

    /**
     * 查找过期的知识
     */
    @Query("SELECT kb FROM KnowledgeBase kb WHERE kb.lifeEntityId = :lifeEntityId AND kb.expiresAt IS NOT NULL AND kb.expiresAt < :now")
    List<KnowledgeBase> findExpiredKnowledge(@Param("lifeEntityId") Long lifeEntityId, 
                                            @Param("now") LocalDateTime now);

    /**
     * 查找需要验证的知识
     */
    @Query("SELECT kb FROM KnowledgeBase kb WHERE kb.lifeEntityId = :lifeEntityId AND kb.status = 'LEARNING'")
    List<KnowledgeBase> findKnowledgeNeedingValidation(@Param("lifeEntityId") Long lifeEntityId);

    /**
     * 分页查询知识
     */
    Page<KnowledgeBase> findByLifeEntityId(Long lifeEntityId, Pageable pageable);

    /**
     * 统计知识数量
     */
    @Query("SELECT COUNT(kb) FROM KnowledgeBase kb WHERE kb.lifeEntityId = :lifeEntityId AND kb.status = :status")
    Long countByLifeEntityIdAndStatus(@Param("lifeEntityId") Long lifeEntityId, @Param("status") KnowledgeStatus status);

    /**
     * 计算平均置信度
     */
    @Query("SELECT AVG(kb.confidence) FROM KnowledgeBase kb WHERE kb.lifeEntityId = :lifeEntityId AND kb.status = 'VALIDATED'")
    Double calculateAverageConfidence(@Param("lifeEntityId") Long lifeEntityId);

    /**
     * 查找相关知识
     */
    @Query("SELECT kb FROM KnowledgeBase kb WHERE kb.lifeEntityId = :lifeEntityId AND kb.relatedKnowledgeIds LIKE %:knowledgeId%")
    List<KnowledgeBase> findRelatedKnowledge(@Param("lifeEntityId") Long lifeEntityId, 
                                            @Param("knowledgeId") String knowledgeId);

    /**
     * 全文搜索知识
     */
    @Query("SELECT kb FROM KnowledgeBase kb WHERE kb.lifeEntityId = :lifeEntityId AND (kb.title LIKE %:keyword% OR kb.content LIKE %:keyword% OR kb.summary LIKE %:keyword%)")
    List<KnowledgeBase> searchKnowledge(@Param("lifeEntityId") Long lifeEntityId, 
                                       @Param("keyword") String keyword);

    /**
     * 查找成功应用率高的知识
     */
    @Query("SELECT kb FROM KnowledgeBase kb WHERE kb.lifeEntityId = :lifeEntityId AND kb.successfulApplications > 0 AND (kb.successfulApplications * 1.0 / (kb.successfulApplications + kb.failedApplications)) >= :minSuccessRate")
    List<KnowledgeBase> findHighSuccessRateKnowledge(@Param("lifeEntityId") Long lifeEntityId, 
                                                     @Param("minSuccessRate") Double minSuccessRate);
}